package com.example.common;

import org.springframework.stereotype.Component;

import java.security.MessageDigest;

@Component
public class MD5Until {

    /**
     * MD5 32位小写加密
     *
     * @param encryptStr 要加密的字符串
     * @return 加密后的字符串
     */
    public static String encrypt32(String encryptStr) {
        MessageDigest md5;
        try {
            md5 = MessageDigest.getInstance("MD5");
            byte[] md5Bytes = md5.digest(encryptStr.getBytes());
            StringBuilder buffer = new StringBuilder();
            for (byte b : md5Bytes) {
                int number = b & 0xff;
                String str = Integer.toHexString(number);
                if (str.length() == 1) {
                    buffer.append("0");
                }
                buffer.append(str);
            }
            encryptStr = buffer.toString();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return encryptStr;
    }

}
